<template>
  <div :class="'chart'+className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts' // echarts theme
import resize from './mixins/resize'
// import { checkTaskYear } from '@/api/remote-search'
require('echarts/theme/macarons')

export default {
  mixins: [resize],
  props: {
    className: {
      type: String
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    PieChartData: {
      type: Object

    }
  },
  data () {
    return {
      chart: null
      // PieChartData: PieChartData
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
    window.addEventListener('resize', () => {
      this.chart.resize()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    PieChartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    }
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.PieChartData)
    },
    setOptions (data) {
      if (data) {
        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            left: 'center',
            bottom: '10',
            data: data.legendData
          },
          series: [
            {
              name: data.seriesName,
              type: 'pie',
              roseType: 'radius',
              radius: [15, 75],
              center: ['50%', '38%'],
              data: data.seriesData,
              animationEasing: 'cubicInOut',
              animationDuration: 2600
            }
          ]
        })
      }
    }
  }

}
</script>
